/**
 * 
 */
package com.sebscape.sebcms.slideshow.actions;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sebscape.sebcms.Constants;
import com.sebscape.sebcms.setup.domain.SetupConfiguration;
import com.sebscape.sebcms.setup.services.SiteConfigurationServices;
import com.sebscape.sebcms.slideshow.domain.GenericSlideShow;
import com.sebscape.sebcms.slideshow.persistence.SlideShowDao;
import com.sebscape.sebcms.tools.Utils;
import com.google.gdata.util.AuthenticationException;

/**
 * @author Stephen
 *
 */
public class SlideShowAction extends ActionSupport {

	private static final long serialVersionUID = 7491443257265004602L;
	private static Log log = LogFactory.getLog( SlideShowAction.class );
	
	private SlideShowDao dao;
	private String serviceUsername;
	private String servicePassword;
	private String albumName;
	
	public String execute(){
		// get the username/password.
		if( Utils.isEmpty(serviceUsername) || Utils.isEmpty( servicePassword ) ){
			SetupConfiguration sc = SiteConfigurationServices
				.findConfiguration(ServletActionContext.getServletContext());
			this.serviceUsername = sc.getPicasaServiceUsername();
			this.servicePassword = sc.getPicasaServicePassword();
		}
		// get the album name.
		if( Utils.isEmpty(albumName)){
			SetupConfiguration sc = SiteConfigurationServices
			.findConfiguration(ServletActionContext.getServletContext());
			this.albumName = sc.getPicasaDefaultAlbumName();
		}
		try {
			log.debug( "PicassaDAO will use id / password: " + serviceUsername + " / " + servicePassword 
					+ " for albumName \"" + albumName + "\"" );
			this.dao = new SlideShowDao( serviceUsername, servicePassword );
			log.debug( "dao.getIsConnected() = " + dao.getIsConnected() );
			GenericSlideShow show = dao.getSlideShow(albumName);
			ServletActionContext.getRequest().setAttribute( Constants.ATTRIB_SLIDESHOW, show );
			return SUCCESS;
		} catch (AuthenticationException e) {
			e.printStackTrace();
			return ERROR;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * @param serviceUsername the serviceUsername to set
	 */
	public void setServiceUsername(String serviceUsername) {
		this.serviceUsername = serviceUsername;
	}

	/**
	 * @param servicePassword the servicePassword to set
	 */
	public void setServicePassword(String servicePassword) {
		this.servicePassword = servicePassword;
	}

	/**
	 * @param albumName the albumName to set
	 */
	public void setAlbumName(String albumName) {
		this.albumName = albumName;
	}
	
	
}
